--
-- Created by IntelliJ IDEA.
-- User: bluda
-- Date: 2018/8/20
-- Time: 21:24
-- To change this template use File | Settings | File Templates.
--
-- 近战
cc.exports.NEAR_ATTACK = 0
-- 远程
cc.exports.FAR_ATTACK = 1

cc.exports.ANIM_FRAMES = 8

cc.exports.FACE_LEFT = -1
cc.exports.FACE_RIGHT = 1

cc.exports.BATTELFIELD_GRID = 150

cc.exports.NEAR_GRID_CHOOSE = {
    {0,0},
    {1,0},
    {1,1},
    {0,1},
    {-1,1},
    {-1,0},
    {-1,-1},
    {0,-1},
    {1,-1},
    {1,-1},

    {2,0},
    {2,1},
    --{2,2}, 
    {1,2},
    {0,2},
    {-1,2},
    --{-2,2}, 
    {-2,1},
    {-2,0},
    {-2,-1},
    --{-2,-2}, 
    {-1,-2},
    {0,-2},
    {1,-2},
    --{2,-2}, 
    {2,-1},
}

function cc.exports.sleep(n)
    local t0 = os.clock()
    while os.clock() - t0 <= n do end
end

function cc.exports.Set()
    local reverse = {} --以数据为key，数据在set中的位置为value
    local set = {} --一个数组，其中的value就是要管理的数据
    return setmetatable(set,{__index = {
        insert = function(set,value)
            if not reverse[value] then
                table.insert(set,value)
                reverse[value] = table.getn(set)
            end
        end,

        remove = function(set,value)
            local index = reverse[value]
            if index then
                reverse[value] = nil
                local top = table.remove(set) --删除数组中最后一个元素
                if top ~= value then
                    --若不是要删除的值，则替换它
                    reverse[top] = index
                    set[index] = top
                end
            end
        end,

        find = function(set,value)
            local index = reverse[value]
            return (index and true or false)
        end,
    }})
end

function cc.exports.logD(...)
    return
    --print(...)
end

function cc.exports.logI(...)
    --print(...)
end

function cc.exports.logW(...)
    print(...)
end


function cc.exports.xcopy(obj)
    local InTable = {}

    local function Func(obj)
        if type(obj) ~= "table" then
            return obj
        end
        local NewTable = {}
        InTable[obj] = NewTable
        for k,v in pairs(obj) do
            NewTable[Func(k)] = Func(v)
        end
        return setmetatable(NewTable, getmetatable(obj))
    end

    return Func(obj)
end

function cc.exports.extend(obj)
    local InTable = {}

    local function Func(obj)
        if type(obj) ~= "table" then
            return obj
        end
        local NewTable = {}
        InTable[obj] = NewTable
        for k,v in pairs(obj) do
            NewTable[Func(k)] = Func(v)
        end
        return setmetatable(NewTable, getmetatable(obj))
    end

    return Func(obj)
end

function cc.exports.distance(p1, p2)
    return  math.sqrt(math.pow((p2.y-p1.y),2)+math.pow((p2.x-p1.x),2))
end

function cc.exports.distance4(x1, y1, x2, y2)
    return  math.sqrt(math.pow((y2-y1),2)+math.pow((x2-x1),2))
end

function cc.exports.signed(p1, p2) 
    if p2-p1 == 0 then
        return 0
    end
    return (p2-p1)/(math.abs(p2-p1)) 
end

function cc.exports.angleY(p1, p2)
    local p = {}
    p.x = p2.x - p1.x
    p.y = p2.y - p1.y

    local r = math.atan2(p.y,p.x)*180/math.pi
    if r < 0 then
        return 180-math.abs(r)
    end    
    return r
end

function cc.exports.angleX(p1, p2)
    local p = {}
    p.x = p2.x - p1.x
    p.y = p2.y - p1.y

    local r = math.atan2(p.y,p.x)*180/math.pi
    if r < 0 then
        return 180-math.abs(r)
    end
    return r
end

function cc.exports.angle(p1, p2)
    local p = {}
    p.x = p2.x - p1.x
    p.y = p2.y - p1.y

    local r = math.atan2(p.y,p.x)*180/math.pi
    if r < 0 then
        return 360-math.abs(r)
    end
    return r
end

cc.exports.win = cc.Director:getInstance():getWinSize()

--
--function cc.exports.QuadraticTrail(x, p1, p2, top) 
--    local a = -4*top / math.pow(p1.x-p2.x, 2)
--
--    return a * math.pow((x - (p1.x+p2.x)/2),2) + p2.y + top
--end


function cc.exports.QuadraticTrail(dist, p1, p2, signed)
--    local a = (x-p1.x)/(p2.x-p1.x)
--    return a * (p2.y-p1.y) + p1.y
    
    local thetaX = (p2.y-p1.y) / (p2.x-p1.x)
    local thetaY = 1 / thetaX

    return math.sqrt(dist*dist/(1+(thetaX*thetaX)), 2) * signed.x, math.sqrt(dist*dist/(1+(thetaY*thetaY)), 2) * signed.y
end

print(cc.exports.QuadraticTrail(2,{x=0,y=0},{x=5,y=5},{x=1,y=1}))

function cc.exports.xprint(tbl ,maxLevel, level, filteDefault)
    local msg = ""
    filteDefault = filteDefault or true 
    level = level or 1
    maxLevel = maxLevel or 999999999
    local indent_str = ""
    for i = 1, level do
        indent_str = indent_str.."  "
    end

    if tbl == nil then
        print("table is nil.")
        return
    end
    print(indent_str .. "{")
    for k,v in pairs(tbl) do
        if filteDefault then
            if k ~= "_class_type" and k ~= "DeleteMe" then
                local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v))
                print(item_str)
                if type(v) == "table" and level < maxLevel then
                    xprint(v, maxLevel, level + 1)
                end
            end
        else
            local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v))
            print(item_str)
            if type(v) == "table" and level < maxLevel then
                xprint(v, maxLevel, level + 1)
            end
        end
    end
    print(indent_str .. "}")
end

function cc.exports.inNode(node, position)
    local nodeSize = node:getContentSize()
    local x,y = node:getPosition()
    --print(position.x, position.y, x,y, nodeSize.width, nodeSize.height)
    if (position.x >= 0 and nodeSize.width) and (position.y >= 0 and position.y <= nodeSize.height)  then
        return true    
    end
    
    return false
end

local axx = {
    
    a = 123,
    b = function() print("123") end,
    c= {
        d="123"
    }
}

xprint(axx)



--local h = p2.y - p1.y
--local l = p2.x - p1.x
--local r = distance(p2, p1)
--local t = l * h / r
--local ox = p1.x + t
--local oy = p2.y + p2.x - p1.x - t
--
--local a = (p2.y - oy) / math.pow(p2.x-ox, 2)
--
--return a * math.pow((x - ox),2) + oy

--local p1 = {x=-1,y=-1 }
--
--local p2 = {x=1,y=-1 }
--
--local x = 0
--
--local top = 100
--
--print(QuadraticTrail(x, p1, p2, top))


local test = { 2, 3, 4, 8, 9, 100, 20, 13, 15, 7, 11 }
local count = 1
for i = 1, #test do
    local vx = test[count]
    print(count, vx)
    if vx % 2 == 0 then
        table.remove(test, count)
        count = count - 1
    end
    count = count + 1
end

for i, v in ipairs( test ) do
    print(i .. "====" .. v)
end



import("app.Test")